package com.web.manager;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.domain.ApiResponse;
import com.web.entity.Member;
import com.web.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author FDjavaone
 * @date 2017/8/22
 */
public class IdCardManager {
    public static ApiResponse getInfo(Member member) {
        ApiResponse ponse=new ApiResponse();
        String host = "http://idcard.market.alicloudapi.com";
        String path = "/lianzhuo/idcard";
        String method = "GET";
        String appCode = "2367dde9fcb5418c86b13a0bd8a3e7f6";
        Map<String, String> headers = new HashMap();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap();
        querys.put("cardno", member.getIdentityNo());
        querys.put("name", member.getName());
        try {

            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String resultBody=EntityUtils.toString(response.getEntity());

            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode;

            rootNode = objectMapper.readValue(resultBody, JsonNode.class);

            JsonNode respNode=rootNode.get("resp");
            String resultCode = respNode.get("code").asText();
            // 一致
            if ("0".equals(resultCode)) {
                JsonNode data = rootNode.get("data");
                Map params=new HashMap();
                params.put("sex",data.get("sex").textValue());
                params.put("address",data.get("address").textValue());
                ponse.setStatus(900000);
                ponse.setMessage("匹配");
                ponse.setParams(params);
            }else if("5".equals(resultCode)){
                //不匹配
                ponse.setStatus(900101);
                ponse.setMessage("不匹配");
            }else if("14".equals(resultCode)){
                //无此身份证号码
                ponse.setStatus(900102);
                ponse.setMessage("无此身份证号码");
            }else if("96".equals(resultCode)){
                //交易失败，请稍后重试
                ponse.setStatus(900103);
                ponse.setMessage("交易失败，请稍后重试");
            }else{
                ponse.setStatus(900104);
                ponse.setMessage("未知错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ponse;
    }
}
